package com.doushu.dispatch.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.doushu.dispatch.common.constans.RedisCode;
import com.doushu.dispatch.dto.*;
import com.doushu.dispatch.entity.*;
import com.doushu.dispatch.mapper.*;
import com.doushu.dispatch.common.dto.ResultDto;
import com.doushu.dispatch.common.dto.ResultDtoFactory;
import com.doushu.dispatch.service.DataUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class DataUpServiceImpl implements DataUpService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @SuppressWarnings("all")
    @Autowired
    private ItemMapper itemMapper;

    @SuppressWarnings("all")
    @Autowired
    private KeywordZTCMapper keywordZTCMapper;

    @SuppressWarnings("all")
    @Autowired
    private ItemFromMapper itemFromMapper;

    @SuppressWarnings("all")
    @Autowired
    private KeywordAnalysisMapper keywordAnalysisMapper;

    @SuppressWarnings("all")
    @Autowired
    private CreativeMapper creativeMapper;

    public ResultDto itemsEffectDetailUp(String data, Long shopId) {
        List<ItemEffectDto> list = JSON.parseObject(data, new TypeReference<List<ItemEffectDto>>() {
        });
        for (ItemEffectDto d : list) {
            Item item = new Item();
            item.setItemId(d.getId());
            item.setSid(shopId);
            item.setTitle(d.getTitle());
            item.setItemDetailUrl(d.getItemDetailUrl());
            item.setImgUrl(d.getPictUrl());
            item.setPrice(d.getReservePrice());
            item.setCreateTime(d.getPublishTime());
            String key = RedisCode.prefix + d.getId();
            if (stringRedisTemplate.hasKey(key)) {
                itemMapper.updateItem(item);
            } else {
                itemMapper.insertItem(item);
                stringRedisTemplate.opsForValue().set(key, "1");
            }
        }
        return ResultDtoFactory.toAck("success", true);
    }

    public ResultDto carDataUp(String data, Long shopId, Integer terminal, String datatime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(datatime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<KeywordDto> list = JSON.parseObject(data, new TypeReference<List<KeywordDto>>() {
        });
        for (KeywordDto d : list) {
            String key = RedisCode.prefix + shopId + "_" + datatime + "_" + d.getIid() + "_" + d.getBidwordid() + terminal;
            if (!stringRedisTemplate.hasKey(key)) {
                KeywordZTC keyword = new KeywordZTC();
                keyword.setSid(shopId);
                keyword.setDatatime(time);
                keyword.setTerminal(terminal);
                keyword.setTransactionshippingtotal(d.getTransactionshippingtotal());
                keyword.setCost(d.getCost());
                keyword.setClick(d.getClick());
                keyword.setFavtotal(d.getFavtotal());
                keyword.setCarttotal(d.getCarttotal());
                keyword.setBidwordstr(d.getBidwordstr());
                keyword.setLinkurl(d.getLinkurl());
                keyword.setBidwordid(d.getBidwordid());
                keyword.setIid(d.getIid());
                keywordZTCMapper.insertKeyword(keyword);
                stringRedisTemplate.opsForValue().set(key, "1");
            }
        }
        return ResultDtoFactory.toAck("success", true);
    }

    public ResultDto listFlowFromUp(String data, Long shopId, Long itemId, Integer terminal, String datatime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(datatime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<ItemFromDto> list = JSON.parseObject(data, new TypeReference<List<ItemFromDto>>() {
        });
        for (ItemFromDto d:list){
            String key = RedisCode.prefix + datatime + '_' + itemId + '_' + d.getSourceId() + '_' + terminal;
            if(!stringRedisTemplate.hasKey(key)){
                ItemFrom itemFrom = new ItemFrom();
                itemFrom.setSourceName(d.getSourceName());
                itemFrom.setPayBuyerCnt(d.getPayBuyerCnt());
                itemFrom.setPayRate(d.getPayRate());
                itemFrom.setFavBuyerCnt(d.getFavBuyerCnt());
                itemFrom.setSourceId(d.getSourceId());
                itemFrom.setAddCartBuyerCnt(d.getAddCartBuyerCnt());
                itemFrom.setUv(d.getUv());
                itemFrom.setPv(d.getPv());
                itemFrom.setIid(itemId);
                itemFrom.setDatatime(time);
                itemFrom.setTerminal(terminal);
                itemFrom.setSid(shopId);
                itemFromMapper.insertItemFrom(itemFrom);
                stringRedisTemplate.opsForValue().set(key, "1");
            }
        }
        return ResultDtoFactory.toAck("success", true);
    }

    public ResultDto itemKeywordsUp(String data, Long shopId, Long itemId, Integer terminal, String datatime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(datatime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<KeywordAnalysisDto> list = JSON.parseObject(data, new TypeReference<List<KeywordAnalysisDto>>() {
        });
        for(KeywordAnalysisDto d:list){
            String key = RedisCode.prefix + datatime + "_" + itemId + "_" + d.getKeyword() + "_" + terminal;
            if(!stringRedisTemplate.hasKey(key)) {
                KeywordAnalysis keyword = new KeywordAnalysis();
                keyword.setPayAmt(d.getPayAmt());
                keyword.setPayBuyerCnt(d.getPayBuyerCnt());
                keyword.setPayRate(d.getPayRate());
                keyword.setKeyword(d.getKeyword());
                keyword.setUv(d.getUv());
                keyword.setPayItemQty(d.getPayItemQty());
                keyword.setBounceRate(d.getBounceRate());
                keyword.setPv(d.getPv());
                keyword.setAvgPv(d.getAvgPv());
                keyword.setClickCnt(d.getClickCnt());
                keyword.setClickRate(d.getClickRate());
                keyword.setExpose(d.getExpose());
                keyword.setAvgSeRank(d.getAvgSeRank());
                keyword.setIid(itemId);
                keyword.setDatatime(time);
                keyword.setTerminal(terminal);
                keyword.setSid(shopId);
                keywordAnalysisMapper.insertKeywordAnalysis(keyword);
                stringRedisTemplate.opsForValue().set(key, "1");
            }
        }
        return ResultDtoFactory.toAck("success", true);
    }

    @Override
    public ResultDto creativeUp(Long shopId, Integer campaignId, Integer adGroupId, String datatime, String trafficType, String data) {
        String key = RedisCode.prefix + shopId + "_" + adGroupId + "_" + datatime + "_" + trafficType;
        if(!stringRedisTemplate.hasKey(key)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date time = null;
            try {
                time = sdf.parse(datatime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            CreativeDto creativeDto = JSON.parseObject(data, new TypeReference<CreativeDto>() {
            });
            Creative creative = new Creative();
            creative.setAdgroupid(adGroupId);
            creative.setCampaignid(campaignId);
            creative.setCarttotal(creativeDto.getCarttotal());
            creative.setClick(creativeDto.getClick());
            creative.setCost(creativeDto.getCost());
            creative.setCoverage(creativeDto.getCoverage());
            creative.setCpc(creativeDto.getCpc());
            creative.setCpm(creativeDto.getCpm());
            creative.setCtr(creativeDto.getCtr());
            creative.setDatatime(time);
            creative.setDirectcarttotal(creativeDto.getDirectcarttotal());
            creative.setDirecttransaction(creativeDto.getDirecttransaction());
            creative.setDirecttransactionshipping(creativeDto.getDirecttransactionshipping());
            creative.setFavitemtotal(creativeDto.getFavitemtotal());
            creative.setFavshoptotal(creativeDto.getFavshoptotal());
            creative.setFavtotal(creativeDto.getFavtotal());
            creative.setImpression(creativeDto.getImpression());
            creative.setIndirectcarttotal(creativeDto.getIndirectcarttotal());
            creative.setIndirecttransaction(creativeDto.getIndirecttransaction());
            creative.setIndirecttransactionshipping(creativeDto.getIndirecttransactionshipping());
            creative.setRoi(creativeDto.getRoi());
            creative.setSid(shopId);
            creative.setTraffictype(trafficType);
            creative.setTransactionshippingtotal(creativeDto.getTransactionshippingtotal());
            creative.setTransactiontotal(creativeDto.getTransactiontotal());
            creativeMapper.insertItem(creative);
            stringRedisTemplate.opsForValue().set(key, "1");
        }
        return ResultDtoFactory.toAck("success", true);
    }
}
